//
//  SPManageSearchView.swift
//  SparklingPets
//
//  Created by iOS-Czz on 2023/10/26.
//

import UIKit

class SPManageSearchView: UIView {
    @IBOutlet var dateTF: UITextField!
    @IBOutlet var nameTF: UITextField!
    @IBOutlet var timeTF: UITextField!
    var onSave: ((_ searchDic:[String:String]) -> Void)?

    @IBAction func clickSelectDateAction(_ sender: Any) {
        self.endEditing(true)
        let dialog = Dialog()
        dialog.wDateTimeTypeSet()("yyyy-MM-dd")
        .wDefaultDateSet()(Date())
        .wCancelTitleSet()("Cancel")
        .wOKTitleSet()("Ok")
        .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
            self.dateTF.text = otherData as? String
        })
        .wTypeSet()(DialogTypeDatePicker);
        _ = dialog.wStart()
    }
    @IBAction func clcickSelectPlanTimeAction(_ sender: Any) {
        self.endEditing(true)
        let dialog = Dialog()
        dialog.wDateTimeTypeSet()("HH:mm")
        .wDefaultDateSet()(Date())
        .wCancelTitleSet()("Cancel")
        .wOKTitleSet()("Ok")
        .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
            if let formattedTime = Date.formatTime(inputTime: otherData as! String) {
                self.timeTF.text = formattedTime
            } else {
                print("Invalid time format")
            }
        })
        .wTypeSet()(DialogTypeDatePicker);
        _ = dialog.wStart()
    }
    @IBAction func clickResetFilterAction(_ sender: Any) {
        self.dateTF.text = ""
        self.nameTF.text = ""
        self.timeTF.text = ""
    }
    @IBAction func clickFindNowAction(_ sender: Any) {
        var sql = ["date":"","name":"","time":""]
        sql["date"] = self.dateTF.text!.isEmpty ? "":self.dateTF.text
        sql["name"] = self.nameTF.text!.isEmpty ? "":self.nameTF.text
        sql["time"] = self.timeTF.text!.isEmpty ? "":self.timeTF.text
        onSave?(sql)
        self.removeFromSuperview()
    }
}
